﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SocialLearning
{
    public partial class MainForm : Form
    {
        #region Public Methods

        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent(); // Load initial components
            statusStripComputerName.Text = string.Format("COMPUTER\\{0}", SystemInformation.ComputerName); // Get computer name           
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Handles the Load event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            UAC_Engine.PopulateLists(); // Populate 
            AddTreeViewNodes(); // Add initial nodes
        }

        public void LoadGroup(List<string> group)
        {
            dgvExplorer.Columns.Clear();
            dgvExplorer.Columns.Add("1", "ID");
            dgvExplorer.Columns.Add("2", "Group name");
            int count = 0;
            foreach (var GroupName in group)
            {
                count++;
                dgvExplorer.Rows.Add(count.ToString(), GroupName);
            }
        }

        private void LoadUsers()
        {
            dgvExplorer.Columns.Clear();
            dgvExplorer.Columns.Add("1", "Number");
            dgvExplorer.Columns.Add("2", "Account ID");
            dgvExplorer.Columns.Add("3", "ID number");
            dgvExplorer.Columns.Add("4", "First name");
            dgvExplorer.Columns.Add("5", "Last name");
            dgvExplorer.Columns.Add("6", "Gender");
            dgvExplorer.Columns.Add("7", "Email");
            dgvExplorer.Columns.Add("8", "Date Of Birth");
            dgvExplorer.Columns.Add("9", "Home Language");
            dgvExplorer.Columns.Add("10", "Nationality");
            dgvExplorer.Columns.Add("11", "Home Address");
            dgvExplorer.Columns.Add("12", "Postal Address");
            dgvExplorer.Columns.Add("13", "City");
            dgvExplorer.Columns.Add("14", "Country");
            dgvExplorer.Columns.Add("15", "Code");
            dgvExplorer.Columns.Add("16", "Medical Aid");
            dgvExplorer.Columns.Add("17", "MedicalAidNumber");
            dgvExplorer.Columns.Add("18", "EthnicGroup");
            dgvExplorer.Columns.Add("19", "Disabilities");

            int count = 0;
            UAC_UserManagement um = new UAC_UserManagement();
            //um.LoadUserList();
            foreach (var user in UAC_UserManagement.userlist)
            {
                count++;
                dgvExplorer.Rows.Add(count.ToString(), user.AccountUID, user.BasicAccountDetails.ID, user.BasicAccountDetails.Firstname, user.BasicAccountDetails.Lastname,
                    user.BasicAccountDetails.Gender, user.BasicAccountDetails.Email, user.AdvancedAccountDetails.DateOfBirth, user.AdvancedAccountDetails.HomeLanguage,
                    user.AdvancedAccountDetails.Nationality, user.AdvancedAccountDetails.HomeAddress, user.AdvancedAccountDetails.PostalAddress,
                    user.AdvancedAccountDetails.City, user.AdvancedAccountDetails.Country, user.AdvancedAccountDetails.Code, user.AdvancedAccountDetails.MedicalAid,
                    user.AdvancedAccountDetails.MedicalAidNumber, user.AdvancedAccountDetails.EthnicGroup, user.AdvancedAccountDetails.Disabilities);
            }
        }

        /// <summary>
        /// Adds the tree view nodes.
        /// </summary>
        private void AddTreeViewNodes()
        {
            try
            {
                treeView.BeginUpdate();
                treeView.ImageList = imageList; // Set image list.
                treeView.ImageIndex = 0;
                treeView.SelectedImageIndex = 1;
                treeView.Nodes.Add(null, SystemInformation.ComputerName, 0);
                treeView.Nodes[0].Nodes.Add(null, "UAC_USER_MANAGEMENT", 0);
                treeView.Nodes[0].Nodes.Add(null, "UAC_GROUP_MANAGEMENT", 0);
                treeView.Nodes[0].Nodes.Add(null, "UAC_GLOBAL_FLAGS", 0);
            }
            catch (Exception e)
            {
                MessageBox.Show(string.Format("Error: {0}", e));
            }
        }

        #endregion

        #region Private Controls

        /// <summary>
        /// Handles the Click event of the exitToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Handles the Click event of the addNodesToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void addNodesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddTreeViewNodes(); // Add initial nodes
        }

        /// <summary>
        /// Handles the Click event of the newUserToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void newUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (UserNewForm newUserForm = new UserNewForm { Text = "New User*", AutoScroll = true, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
            {
                newUserForm.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the Click event of the updateUserToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void updateUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadUsers();
        }

        /// <summary>
        /// Handles the Click event of the deleteUserToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void deleteUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (UserDeleteForm deleteUserForm = new UserDeleteForm { Text = "Delete User!", AutoScroll = true, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
            {
                deleteUserForm.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the Click event of the newGroupToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void newGroupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (GroupNewForm newGroupForm = new GroupNewForm { Text = "New Group*", AutoScroll = true, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
            {
                newGroupForm.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the Click event of the updateGroupToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void updateGroupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (GroupUpdateForm updateGroupForm = new GroupUpdateForm { Text = "Update Group?", AutoScroll = true, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
            {
                updateGroupForm.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the Click event of the deleteGroupToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void deleteGroupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (GroupDeleteForm deleteGroupForm = new GroupDeleteForm { Text = "Delete Group!", AutoScroll = true, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
            {
                deleteGroupForm.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the Click event of the aboutToolStripMenuItem1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            using (AboutForm aboutForm = new AboutForm { Text = "About UAC Explorer", AutoScroll = false, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
            {
                aboutForm.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the Click event of the roleToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void roleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (RoleNewForm roleNewForm = new RoleNewForm { Text = "Role member?", AutoScroll = true, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
            {
                roleNewForm.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the 1 event of the aboutToolStripMenuItem1_Click control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void aboutToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            using (AboutForm aboutForm = new AboutForm { Text = "About UAC Explorer", AutoScroll = false, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
            {
                aboutForm.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the AfterSelect event of the treeView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView.SelectedNode.Text == "UAC_GROUP_MANAGEMENT")
            {
                dgvExplorer.Columns.Clear();
                dgvExplorer.Columns.Add("1", "ID");
                dgvExplorer.Columns.Add("2", "Group name");
                int count = 0;
                foreach (var GroupName in UAC_GroupManagement.Instance.SeeGroup())
                {
                    count++;
                    dgvExplorer.Rows.Add(count.ToString(), GroupName);
                }
            }
            else if (treeView.SelectedNode.Text == "UAC_GLOBAL_FLAGS")
            {
                dgvExplorer.Columns.Clear();
                dgvExplorer.Columns.Add("1", "ID");
                dgvExplorer.Columns.Add("2", "Flag name");
                int count = 0;
                foreach (var FlagName in Enum.GetNames(typeof(UAC_AccessFlags)))
                {
                    count++;
                    dgvExplorer.Rows.Add(count.ToString(), FlagName);
                }
            }
            else if (treeView.SelectedNode.Text == "UAC_USER_MANAGEMENT")
            {
                //  treeView.SelectedNode = treeView.Nodes.Find("UAC_GLOBAL_FLAGS", true)[0];
                LoadUsers();
            }
        }

        /// <summary>
        /// Handles the DoubleClick event of the dgvExplorer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void dgvExplorer_DoubleClick(object sender, EventArgs e)
        {
            if (treeView.SelectedNode.Text != "UAC_GLOBAL_FLAGS")
            {
                if (treeView.SelectedNode.Text == "UAC_GROUP_MANAGEMENT" && dgvExplorer.Rows.Count != 0)
                {
                    List<string> templist =
                      UAC_GroupManagement.Instance.SeeGroupMembers(dgvExplorer.CurrentRow.Cells[1].Value.ToString());
                    dgvExplorer.Columns.Clear();
                    dgvExplorer.Columns.Add("1", "ID");
                    dgvExplorer.Columns.Add("2", "User name");
                    int count = 0;
                    foreach (string GroupName in templist)
                    {
                        count++;
                        dgvExplorer.Rows.Add(count.ToString(), GroupName);
                    }
                }
                else if (dgvExplorer.Rows.Count != 0)
                    using (CutomMessageForm customMessageForm = new CutomMessageForm(dgvExplorer.CurrentRow.Index, this, treeView.SelectedNode.Text) { Text = "Custom message", AutoScroll = true, StartPosition = FormStartPosition.CenterScreen, ControlBox = false })
                    {
                        customMessageForm.ShowDialog();
                    }
            }
        }

        #endregion
    }
}
